﻿/// <reference path="../General/General.js" />
/// <reference path="PropertyDefinition.js" />
/// <reference path="../Turbo/TurboBase.js" />

var $BaseControl = TA.Turbo.Controls.BaseControl = function BaseControl(clientId)
{
	if (Function.IsInherit(arguments)) return;

	this._controlId = clientId;
}

$BaseControl.prototype._controlId = null;
$BaseControl.prototype._control = null;
$BaseControl.prototype._properties = new Object();
$BaseControl.prototype._propertyDefinitions = new Object();
$BaseControl.prototype._propertyShortNames = new Object();

var ____a;
if (____a)
{
	$BaseControl.prototype = new $ServerEventSender();
	$BaseControl.prototype.ServerEventSender = new $ServerEventSender();

	$BaseControl.prototype._control = document.getElementById("asd");
	$BaseControl.prototype._properties = new Object();
	$BaseControl.prototype._propertyDefinitions = new Object();
}

$PropertyDefinition.Define($BaseControl, "Enabled", "E",
true, {
	GetInitValue: function GetInitValue(obj)
	{
		if (obj.get_Control().disabled !== undefined && obj.get_Control().disabled !== null)
			return !obj.get_Control().disabled;
		if (obj.get_Control().getAttribute("disabled") != null)
			return false;
		return true;
	},
	OnChanged: function OnChanged()
	{
		this.get_Control().disabled = !this._properties.Enabled;

		$BaseControl._EnabledChangedRecursive(this.get_Control().childNodes, this._properties.Enabled);

	}
});
$BaseControl._EnabledChangedRecursive = function _EnabledChangedRecursive(nodes, value)
{
	for (var i = 0; i < nodes.length; ++i)
	{
		var child = nodes[i];

		if (child.tagName == null) continue;

		if (value)
		{
			if (child.__saveEnabled !== null && child.__saveEnabled !== undefined)
			{
				$BaseControl._EnabledChangedRecursive_Set(child, child.__saveEnabled);
				delete child.__saveEnabled;
			}
			else
			{
				$BaseControl._EnabledChangedRecursive_Set(child, true);
			}
		}
		else
		{
			if (child.__saveEnabled === null || child.__saveEnabled === undefined)
				child.__saveEnabled = $BaseControl._EnabledChangedRecursive_Get(child);

			$BaseControl._EnabledChangedRecursive_Set(child, false);
		}

		if (child.get_Object() == null)
		{
			$BaseControl._EnabledChangedRecursive(child.childNodes, value);
		}
	}
}
$BaseControl._EnabledChangedRecursive_Get = function _EnabledChangedRecursive_Get(element)
{
	if (element.get_Object() == null)
	{
		return (element.disabled !== true);
	}
	else
	{
		return element.get_Object().get_Enabled();
	}
}
$BaseControl._EnabledChangedRecursive_Set = function _EnabledChangedRecursive_Set(element, value)
{
	if (element.get_Object() == null)
	{
		element.disabled = !value;
	}
	else
	{
		element.get_Object().set_Enabled(value);
	}
}


//$PropertyDefinition.Define($BaseControl, "Visible", "V", true, 
$PropertyDefinition.Define($BaseControl, "Shown", "W", true,
{
	GetInitValue: function GetInitValue(obj)
	{
		return obj.get_Control().style["display"] != "none";
	},
	OnChanged: function OnChanged()
	{
		//		if (this._properties.Visible)
		if (this._properties.Shown)
		{
			if (this.get_Control()._saveStyleDisplay == null)
				this.get_Control()._saveStyleDisplay = "";
			this.get_Control().style["display"] = this.get_Control()._saveStyleDisplay;
		}
		else
		{
			if (this.get_Control().style["display"] != "none")
				this.get_Control()._saveStyleDisplay = this.get_Control().style["display"];
			this.get_Control().style["display"] = "none";
		}
	}
});

$PropertyDefinition.Define($BaseControl, "TabIndex", "I", 0, {
	GetInitValue: function GetInitValue(obj)
	{
		return obj.get_Control().tabIndex;
	},
	OnChanged: function OnChanged()
	{
		this.get_Control().tabIndex = this._properties.TabIndex;
	}
});

$PropertyDefinition.Define($BaseControl, "ToolTip", "T", "",
{
	GetInitValue: function GetInitValue(obj)
	{
		return obj.get_Control().title;
	},
	OnChanged: function OnChanged()
	{
		this.get_Control().title = this._properties.ToolTip;
	}
});

$PropertyDefinition.Define($BaseControl, "CssClass", "C", "",
{
	GetInitValue: function GetInitValue(obj)
	{
		return obj.get_Control().className;
	},
	OnChanged: function OnChanged()
	{
		this.get_Control().className = this._properties.CssClass;
	}
});

$PropertyDefinition.Define($BaseControl, "Style", "S", "",
{
	GetInitValue: function GetInitValue(obj)
	{
		return obj.get_Control().style.cssText;
	},
	OnChanged: function OnChanged()
	{
		this.get_Control().style.cssText = this._properties.Style;
	}
});





TA.Turbo.Controls.BaseControl.get_Object = function get_Object()
{
	return this._object;
}


TA.Turbo.Controls.BaseControl.prototype.get_Control = function get_Control()
{
	return this._control;
	return document.createElement("div");
}


TA.Turbo.Controls.BaseControl.prototype.Init = function Init()
{
	this._control = document.getElementById(this._controlId);
	this._properties = new Object();

	this.get_Control().name = this.get_Control().getAttribute("name");
	this.get_Control()._object = this;
	this.get_Control().get_Object = TA.Turbo.Controls.BaseControl.get_Object;
}

TA.Turbo.Controls.BaseControl.prototype.Load = function Load()
{
	for (var prName in this._propertyDefinitions)
	{
		var pd = this._propertyDefinitions[prName];

		if (this._properties[prName] === undefined) // if not already initialized
			this._properties[prName] = pd._getInitValue(this);
	}
}











Function._standardInherit = Function.prototype.Inherit;
Function.prototype.Inherit = function Inherit(base, baseName)
{
	Function._standardInherit.call(this, base, baseName);

	if (base.prototype._propertyDefinitions != null) // property definitions for turbo
	{
		this.prototype._propertyDefinitions = new Object();
		for (var prName in base.prototype._propertyDefinitions)
			this.prototype._propertyDefinitions[prName] = base.prototype._propertyDefinitions[prName];
	}

	if (base.prototype._propertyShortNames != null) // property definitions for turbo
	{
		this.prototype._propertyShortNames = new Object();
		for (var prShortName in base.prototype._propertyShortNames)
			this.prototype._propertyShortNames[prShortName] = base.prototype._propertyShortNames[prShortName];
	}

	if (base.prototype._eventDefinitions != null) // event definitions for turbo
	{
		this.prototype._eventDefinitions = new Object();
		for (var prName in base.prototype._eventDefinitions)
			this.prototype._eventDefinitions[prName] = base.prototype._eventDefinitions[prName];
	}
}